<template>
<div class="distributor-merchandise views-container">
    <div class="wlm-table">
        <!-- <div class="wlm-table-hearder-btn"> -->
        <!-- <choose-goods ref="chooseGoods" :isShow="false" :config="chooseGoodsConfig" :changeEvt="chooseGoods"></choose-goods> -->
        <!-- </div> -->
        <div class="wlm-table-header">
            <el-form :model="tableFormatData.userTable.files" size="small" label-width="85px" class="retail-form" label-position="right">
                <el-form-item label="" class="filter">
                    <el-form-item label="商品名称：" class="search">
                        <el-input @keyup.enter.native="filesSerch" v-model="tableFormatData.userTable.files.name" placeholder="请输入商品名称" style="width:200px;"></el-input>
                    </el-form-item>
                    <el-form-item label="商品分类：" class="search">
                        <el-cascader :props="myProps" :options="categoryData" :show-all-levels="false" v-model="category_array" @change="categoryChange">
                        </el-cascader>
                    </el-form-item>
                </el-form-item>
                <el-form-item label="" class="filter">
                    <el-form-item label="分销状态：" class="filter">
                        <el-select v-model="tableFormatData.userTable.files.dealer_status" placeholder="请选择">
                            <el-option v-for="item in [{lable:'参与分销',value:'1'},{lable:'不参与分销',value:'0'}]" :key="item.value" :label="item.lable" :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-form-item>
                <el-form-item label="" class="filter-btns">
                    <el-button type="primary" @click="filesSerch">筛选</el-button>
                    <el-button type="text" @click="filesEmptyBefore">清空筛选条件</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="wlm-table-content">
            <el-table :ref="tableFormatData.userTable.key" :data="tableFormatData.userTable.tableData" @selection-change="handleSelectionChange" style="width: 100%">
                <el-table-column type="selection" width="55">
                </el-table-column>
                <el-table-column prop="date" label="商品" min-width="100">
                    <template slot-scope="scope">
                        <div class="wlm-table-logos">
                            <img class="logo" v-if="scope.row.image[0]" :src="scope.row.image[0].file_path">
                            <div class="inner-text flex-col" >
                                <!-- <el-popover class="scale-grow" placement="top-start" width="200" trigger="hover" :content="scope.row.goods_name">
                                    <p slot="reference" style="max-width:125px;" class="ellipsis">{{ scope.row.goods_name }}</p>
                                </el-popover> -->
                                <p style="max-width:175px;" class="ellipsis">{{scope.row.goods_name}}</p>
                                <el-tag style="display:block;width: 48px;" size="mini" v-if="scope.row.spec_type==20">多规格</el-tag>
                            </div>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column prop="date" label="商品分类">
                    <template slot-scope="scope">
                        <span>{{scope.row.category}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="date" label="价格">
                    <template slot-scope="scope">
                        <span class="text-danger" v-if="scope.row.sku.length>0">￥{{scope.row.sku[0].goods_price}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="date" label="默认一级佣金">
                    <template slot-scope="scope">
                        <span v-if="scope.row.dealer_money_type==10">{{scope.row.default_commission}}%</span>
                        <span v-if="scope.row.dealer_money_type==20">￥{{scope.row.default_commission}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="date" label="库存" min-width="45">
                    <template slot-scope="scope">
                        <span v-if="scope.row.spec_type==10&&scope.row.sku.length>0">{{scope.row.sku[0].stock_num}}</span>
                        <span v-if="scope.row.spec_type==20">{{stockAllNum(scope.row.sku)}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="date" label="商品状态" min-width="45">
                    <template slot-scope="scope">
                        <!-- <span v-if="scope.row.goods_status==10">上架</span>
                        <span v-if="scope.row.goods_status==20">下架</span>
                        <span v-if="scope.row.goods_status==30">自定义上架时间</span> -->
                        <span>出售中</span>
                    </template>
                </el-table-column>
                <el-table-column prop="name" label="参与分销" min-width="45">
                    <template slot-scope="scope">
                        <el-switch :active-value="1" :inactive-value="0" 
                        @change="changeTableItem({ids:[scope.row.goods_id],status:scope.row.is_distribution},tableFormatData.userTable.change.is_distribution,
                        {isWarning: scope.row.is_distribution === 1,warning:'是否参与分销？',cancel:{msg:'已取消',val:{scope:scope,cancelStatus:{key:'is_distribution',val:0}}}})" 
                        v-model="scope.row.is_distribution"> </el-switch>
                    </template>
                </el-table-column>
                <el-table-column prop="address" label="操作">
                    <template slot-scope="scope">
                        <div class="operation-group">
                            <router-link class="wlm-text" tag="span" :to="{path:'/distribution/distributorMerchandiseSetting', query:{goods_id:scope.row.goods_id,img:JSON.stringify(scope.row.image)}}">
                                <el-button type="text">设置佣金</el-button>
                            </router-link>
                        </div>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-content flex-row flex-justify-b flex-align-c">
                <el-row class="pagination-btns">
                    <el-checkbox class="check-all" true-label="1" false-label="0" v-model="tableFormatData.userTable.files.checkall" @change="toggleSelection">全部</el-checkbox>
                </el-row>
                <el-pagination :disabled="!hasTableData" @size-change="listPageChange" @current-change="listPageChange" :current-page.sync="tableFormatData.userTable.pagination.page" :page-sizes="tableFormatData.userTable.pagination.pagesizes" :page-size.sync="tableFormatData.userTable.pagination.list_rows" layout="total, sizes, prev, pager, next, jumper" :total="hasTableData?tableFormatData.userTable.pagination.total : 0">
                </el-pagination>
            </div>
        </div>
    </div>
</div>
</template>

<script>
import {
  categoryIndex
} from '@/api/merchandise'
import {
  insertDistributorGoods,
  getDistributorGoods,
  setDistributorGoods
} from '@/api/distribution'
import mixins from '@/mixins/mixins'
// import ChooseGoods from '@/components/ChooseGoods/index'
export default {
  mixins: [mixins.getters('Table')],
  name: 'DistributorMerchandise',
  components: {
    // ChooseGoods
  },
  created() {
    this.$nextTick(() => {
      categoryIndex().then((response) => {
        const { data: { data: msgData = [] } } = response
        this.categoryData = msgData
      })
    })
  },
  computed: {
    stockAllNum() {
      return function(val) {
        let result = 0
        val.map(item => {
          result += item.stock_num
        })
        return result
      }
    }
  },
  data() {
    return {
      myProps: {
        value: 'category_id',
        label: 'name',
        children: 'child'
      },
      category_array: [],
      categoryData: [],
      tableFormatData: {
        current: 'userTable',
        userTable: {
          key: 'userTable',
          api: {
            getList: getDistributorGoods,
            delList: getDistributorGoods
          },
          tableData: [],
          files: {
            name: '',
            category: '',
            // status: '',
            dealer_status: '',
            ids: [],
            checkall: '0',
            Recycle: '0'
          },
          change: {
            is_distribution: {
              key: 'is_distribution',
              api: setDistributorGoods
            }
          },
          pagination: {
            page: 1,
            list_rows: 10,
            pagesizes: [10, 20, 50],
            total: 0
          }
        }
      }
    }
  },
  methods: {
    filesEmptyBefore() {
      this.category_array = []
      this.filesEmpty()
    },
    categoryChange(category_array) {
      const [category_id] = category_array.slice(-1)
      this.tableFormatData.userTable.files.category = category_id
    },
    chooseGoods(params) {
      // console.log(params, 'xxxx')
      if (params.length !== 0) {
        insertDistributorGoods({ ids: params }).then((response) => {
          if (response.data.code === 1) {
            this.$message.success('添加成功！')
            this.filesSerch()
          }
        })
      }
    },
    newFunction(params, id) {
      this.formFormatDialogEditEvt(params)
      this.formFormatData.formData.ids = [id]
    },
    auditBatch(params) {
      this.formFormatDialogEditEvt(params)
      var array = []
      this.tableFormatData.userTable.files.ids.map((item) => {
        array.push(item.goods_id)
      })
      this.formFormatData.formData.ids = array
    }
  }
}
</script>

<style lang="scss" scoped>
.distributor-merchandise {
    .logo {
        height: 32px;
        width: 32px;
        border-radius: 32px;
        vertical-align: middle;
    }
}
</style>
